package com.cloudfun.campusshare.code.generate.definition;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Huqin on 2020/5/26
 */
@Data
public class FieldDefinition {
    private List<AnnotationDefinition> annotations = new ArrayList<>();
    private Class clazz;
    private String name;

    private void init() {
        if (null == clazz) {
            throw new RuntimeException("clazz can not be null");
        }
        if (null == name) {
            throw new RuntimeException("name can not be null");
        }
    }

    public void addAnnotation(AnnotationDefinition annotation) {
        annotations.add(annotation);
    }

    public String toString() {
        init();
        StringBuilder sb = new StringBuilder("   ");
        String annotationDefinitions = annotations.stream().map(AnnotationDefinition::toString).collect(Collectors.joining("\n"));
        sb.append(annotationDefinitions);
        sb.append("\n");
        sb.append(JavaFileDefinition.INDENTS).append("private ").append(clazz.getSimpleName()).append(" ").append(name).append(";\n");
        return sb.toString();
    }
}
